(function(document, undefined) {

var Admin = window.Admin || {};

Admin.Forum = (function(){
  
  var $dlg = $('#forum-dialog');
  var $newButton;
  var dlgOption = {
    autoOpen: false,
    closeOnEscape: true,
    width: 530,
    modal: true,
    position: ['center', 'center'],
    zIndex: 1000
  };
  
  var onDialogClose = function() {
    $dlg.dialog('close');
  };
  
  var addForum = function() {
    var form = $('#forum-form');
    $.ajax({
      url: '/forum/create',
      type: 'post',
      dataType: 'json',
      data: form.serialize(),
      success: function(resp) {
        if (resp.ok) {
          console.log(resp);
        }
      }
    });
  };
  
  var _initUI = function() {
    $newButton = $('#create-button').click(function(e) {
      var opt = dlgOption;
      opt['buttons'] = {
        'Cancel': function() {
          onDialogClose();
        },
        'Create': function() {
          addForum();
          onDialogClose();
        }
      };
      $dlg.attr('title', '新增討論區').dialog(opt).dialog('open');
    });
  };
  
  return {
    init: function() {
      _initUI();
    }
  };
})();

Admin.Forum.init();
  
})(document);

